//
//  FeedbackViewController.m
//  KeepFit
//
//  Created by 杨旭 on 15/11/4.
//  Copyright © 2015年 Fuzzie Liu. All rights reserved.
//

#import "FeedbackViewController.h"
#import <MessageUI/MessageUI.h>//邮件
@interface FeedbackViewController () <MFMailComposeViewControllerDelegate>
@property (nonatomic, retain) UITextField *e_mailTextField;
@property (nonatomic, retain) UITextView *textView;

@end

@implementation FeedbackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"反馈与意见";
    self.navigationController.navigationBar.translucent = NO;//设置导航栏不透明
    self.navigationController.navigationBar.backgroundColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-youjian"] style:UIBarButtonItemStylePlain target:self action:@selector(feedbackAction:)];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, self.view.bounds.size.width - 40, 30)];
    label.text = @"请留下您宝贵的意见";
    label.numberOfLines = 0;
    [label sizeToFit];
    [self.view addSubview:label];
    
    self.textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 60, self.view.bounds.size.width - 40, 150)];
    self.textView.font = [UIFont systemFontOfSize:20];
    self.textView.backgroundColor = [UIColor brownColor];
    [self.view addSubview:self.textView];
    
    UILabel *e_mailLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 220, 50, 30)];
    e_mailLabel.text = @"邮箱";
    [self.view addSubview:e_mailLabel];
    
    self.e_mailTextField = [[UITextField alloc] initWithFrame:CGRectMake(80, 220, self.view.bounds.size.width - 100, 30)];
    self.e_mailTextField.borderStyle = UITextBorderStyleRoundedRect;
    self.e_mailTextField.placeholder = @"请输入您的邮箱";
    self.e_mailTextField.keyboardType = UIKeyboardTypeEmailAddress;
    [self.view addSubview:self.e_mailTextField];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.view addGestureRecognizer:tap];
}

- (void)feedbackAction:(UIButton *)btn{
    NSLog(@"感谢您的反馈");
    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
    
    if (mailClass != nil)
    {
        if ([MFMailComposeViewController canSendMail])
        {
            [self displayComposerSheet];
        }
        else
        {
            [self launchMailAppOnDevice];
        }
    }
    else
    {
        [self launchMailAppOnDevice];
    }
}

//可以发送邮件的话
-(void)displayComposerSheet
{
    MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
    
    mailPicker.mailComposeDelegate = self;
    
    //设置主题
    [mailPicker setSubject: _e_mailTextField.text];
    
    // 添加发送者
    NSArray *toRecipients = [NSArray arrayWithObject: @"2391113904@qq.com"];
    //NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];
    //NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@example.com", nil];
    [mailPicker setToRecipients: toRecipients];
    //[picker setCcRecipients:ccRecipients];
    //[picker setBccRecipients:bccRecipients];
    
    // 添加图片
    UIImage *addPic = [UIImage imageNamed: @"123.jpg"];
    NSData *imageData = UIImagePNGRepresentation(addPic);            // png
    // NSData *imageData = UIImageJPEGRepresentation(addPic, 1);    // jpeg
    [mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"123.jpg"];
    
    NSString *emailBody = _textView.text;
    [mailPicker setMessageBody:emailBody isHTML:YES];
    
    [self presentViewController:mailPicker animated:YES completion:^{
    }];
}

-(void)launchMailAppOnDevice
{
    NSString *recipients = @"2391113904@qq.com";
    //@"mailto:first@example.com?cc=second@example.com,third@example.com&subject=my email!";
    NSString *body = @"&body=email body!";
    
    NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
    email = [email stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
    
    [[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]];
}
- (void)mailComposeController:(MFMailComposeViewController *)controller
          didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    NSString *msg;
    
    switch (result)
    {
        case MFMailComposeResultCancelled:
            msg = @"邮件发送取消";
            break;
        case MFMailComposeResultSaved:
            msg = @"邮件保存成功";
            [self alertWithTitle:nil msg:msg];
            break;
        case MFMailComposeResultSent:
            msg = @"邮件发送成功";
            [self alertWithTitle:nil msg:msg];
            break;
        case MFMailComposeResultFailed:
            msg = @"邮件发送失败";
            [self alertWithTitle:nil msg:msg];
            break;
        default:
            break;
    }
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void) alertWithTitle: (NSString *)_title_ msg: (NSString *)msg
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:_title_
                                                    message:msg
                                                   delegate:nil
                                          cancelButtonTitle:@"确定"
                                          otherButtonTitles:nil];
    [alert show];
}

- (void)tapAction:(UITapGestureRecognizer *)tap{
    [_e_mailTextField resignFirstResponder];
}

- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = NO;//设置 navigationBar隐藏
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
